package com.yougou.ui.register;

import android.content.Intent;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.lifecycle.ViewModelProvider;

import com.google.android.material.button.MaterialButton;
import com.google.android.material.textfield.TextInputEditText;
import com.yougou.MainActivity;
import com.yougou.R;
import com.yougou.base.BaseActivity;
import com.yougou.utils.ToastUtils;

public class RegisterActivity extends BaseActivity {
    
    private ImageView mBtnBack;
    private TextInputEditText mEtUsername;
    private TextInputEditText mEtPhone;
    private TextInputEditText mEtPassword;
    private TextInputEditText mEtConfirmPassword;
    private CheckBox mCbAgree;
    private MaterialButton mBtnRegister;
    private TextView mTvGoLogin;
    
    private RegisterViewModel mViewModel;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        initView();
        initData();
    }
    
    @Override
    protected void initView() {
        mBtnBack = findViewById(R.id.iv_back);
        mEtUsername = findViewById(R.id.et_username);
        mEtPhone = findViewById(R.id.et_phone);
        mEtPassword = findViewById(R.id.et_password);
        mEtConfirmPassword = findViewById(R.id.et_confirm_password);
        mCbAgree = findViewById(R.id.cb_agree);
        mBtnRegister = findViewById(R.id.btn_register);
        mTvGoLogin = findViewById(R.id.tv_go_login);
        
        // 返回按钮
        mBtnBack.setOnClickListener(v -> finish());
        
        // 注册按钮
        mBtnRegister.setOnClickListener(v -> {
            if (!mCbAgree.isChecked()) {
                ToastUtils.show(this, "请先阅读并同意用户协议和隐私政策");
                return;
            }
            
            String username = mEtUsername.getText().toString();
            String phone = mEtPhone.getText().toString();
            String password = mEtPassword.getText().toString();
            String confirmPassword = mEtConfirmPassword.getText().toString();
            
            mViewModel.register(username, phone, password, confirmPassword);
        });
        
        // 去登录
        mTvGoLogin.setOnClickListener(v -> finish());
    }
    
    @Override
    protected void initData() {
        mViewModel = new ViewModelProvider(this).get(RegisterViewModel.class);
        
        // 观察注册结果
        mViewModel.getRegisterResult().observe(this, user -> {
            if (user != null) {
                ToastUtils.show(this, "注册成功");
                // 跳转到主页面
                Intent intent = new Intent(this, MainActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
                finish();
            }
        });
        
        // 观察错误
        mViewModel.getError().observe(this, error -> {
            if (error != null) {
                ToastUtils.show(this, error);
            }
        });
    }
}

